//
//  ControlTemplateViewEvaluationController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewEvaluationController.h"

#pragma mark -
#pragma mark 评价控件【evaluate】
@interface ControlTemplateViewEvaluationController ()
@property (nonatomic, retain) NSString* defaultValue;
@end

@implementation ControlTemplateViewEvaluationController

static ControlTemplateViewEvaluationController* sharedManagerEvaluationInter = nil;

+ (ControlTemplateViewEvaluationController *)sharedManagerEvaluation
{
    sharedManagerEvaluationInter = [[self alloc] init];
    
    return sharedManagerEvaluationInter;
}

- (void)initVar
{
    _valueStr = @"0";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVar];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    _paramId = [_params valueForKey:@"attrInstId"];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel setText:[_params valueForKey:@"title"]];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    
    UIView* classView = [[UIView alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, -5, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
    
    [_bgMainView addSubview:classView];
    classView.backgroundColor = kColor_W;
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"initData"] length] > 0) {
        _defaultValue = [_params valueForKey:@"initData"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag  = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    
    NSArray * initArray = [SEGUtils arrayWithJsonString:[_params valueForKey:@"initData"]];
    NSString * attrValuestring = [_params valueForKey:@"attrValue"];
    _valueStr = attrValuestring;
    NSDictionary * defaultValuedic = initArray.firstObject;
    for (NSDictionary * dic in initArray) {
        NSString * _idStr = dic[@"value"];
        if (attrValuestring.length >0) {
            if ([_idStr isEqualToString:attrValuestring]) {
                defaultValuedic = [[NSDictionary alloc] initWithDictionary:dic];
                break;
            }
        }else{
            if ([dic[@"isDefault"] intValue] == 1) {
                defaultValuedic = [[NSDictionary alloc] initWithDictionary:dic];
                break;
            }
        }
    }
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {
        if (defaultValuedic) {
            UILabel* valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, CGRectGetHeight(_bgMainView.bounds))];
            [valueLabel setFont:kFont_16];
            [valueLabel setTextColor:kColor_1];
            [valueLabel setText:[defaultValuedic valueForKey:@"value"]];
            [_bgMainView addSubview:valueLabel];
        }
    }
    else {
        /**
         *  初始化评价
         */
        
        _valueStr = @"0";
        _optional = @"0";
        
        BOOL isDefault = NO;
        UIImage *imagePre = [UIImage imageWithContentsOfFile:SEGPicName(@"greenStar")];
        UIImage *imageNor = [UIImage imageWithContentsOfFile:SEGPicName(@"grayStar")];
        NSString *defaultValue = @"";
        for (int i = 0; i < initArray.count; i++) {
            NSDictionary *dic = initArray[i];
            UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (imagePre.size.width + 10), (50 - imagePre.size.height) / 2, imagePre.size.width, imagePre.size.height)];
            if (!isDefault) {
                [iconImageView setImage:imagePre];
            } else {
                [iconImageView setImage:imageNor];
            }
            if ([dic[@"isDefault"] intValue] == 1) {
                isDefault = YES;
                defaultValue = dic[@"id"];
                _valueStr = defaultValue;
            }
            [classView addSubview:iconImageView];
            CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(i * (imagePre.size.width + 10), 0, imagePre.size.width + 10, 50)];
            [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            abtn.tag = 100 + i;
            [classView addSubview:abtn];
        }
        if (!isDefault) {
            for (UIImageView* aImageView in [_bgMainView.subviews objectAtIndex:1].subviews) {
                if ([aImageView isKindOfClass:[UIImageView class]]) {
                    [aImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"grayStar")]];
                }
            }
        }
        // 设置初始值,如果attrValue存在，则设置
        NSString *attrValue = ValueToString(_params[@"attrValue"]);
        if (![attrValue isEqualToString:@""]) {
            NSInteger i = 0;
            NSInteger attrValueNumber = [_params[@"attrValue"] integerValue];
            _valueStr = attrValue;
            for (UIImageView* aImageView in [_bgMainView.subviews objectAtIndex:1].subviews) {
                if ([aImageView isKindOfClass:[UIImageView class]]) {
                    if (i < attrValueNumber) {
                        [aImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"greenStar")]];
                        i ++;
                    } else {
                        [aImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"grayStar")]];
                    }
                }
            }
        }
    }
}

- (void)onBtnAction:(CustomButtonView*)sender
{
    int i = 100;
    for (UIImageView* aImageView in [_bgMainView.subviews objectAtIndex:1].subviews) {
        if ([aImageView isKindOfClass:[UIImageView class]]) {
            UIImage* image = nil;
            if (i <= sender.tag) {
                image = [UIImage imageWithContentsOfFile:SEGPicName(@"greenStar")];
            }
            else {
                image = [UIImage imageWithContentsOfFile:SEGPicName(@"grayStar")];
            }
            i++;
            [aImageView setImage:image];
        }
    }
    
    NSArray* valueArray = [SEGUtils arrayWithJsonString:[_params valueForKey:@"initData"]];
    
    NSDictionary* valueStr2 = [valueArray objectAtIndex:(int)sender.tag - 100];
    
    _valueStr = valueStr2[@"id"];
    _optional = @"0";
}

/**
 *  给评论控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingEvaluationView:(NSString*)valueStr
{
    _valueStr = valueStr;
    if ([_valueStr length] > 0) {
        UIView* aview = [_bgMainView.subviews objectAtIndex:1];
        if ([SEGUtils ValueToInteger:_valueStr] <= 5) {
            
            for (int i = 0; i < [SEGUtils ValueToInteger:_valueStr]; i++) {
                UIView *bView = [aview.subviews objectAtIndex:i];
                if ([bView isKindOfClass:[UIImageView class]]) {
                    UIImageView* aimageView = (UIImageView *)bView;
                    [aimageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"greenStar")]];
                }
                
            }
        }
        _optional = @"0";
    }
    else {
        _optional = @"1";
    }
}

@end

